package com.colin.aiyi.dao.ImpxUtils;

import com.colin.aiyi.app.Constant;
import com.colin.aiyi.app.MyApp;
import com.colin.aiyi.entity.response.BaseResponse;
import com.colin.aiyi.entity.User;
import com.colin.aiyi.utils.SharePreferenceUtil;

import org.xutils.http.RequestParams;

import java.io.File;

/**
 * Created by hl on 2017/6/9.
 */

public class ImpUploadImg extends AbsServicexUtils{
    User user;
    @Override
    public void onSuccess(String result) {
        super.onSuccess(result);
        BaseResponse loginResponse=gson.fromJson(result,BaseResponse.class);
        if (loginResponse.getCode()==200){
            user.setHeadUrl(loginResponse.getMessage());
            SharePreferenceUtil.saveObject(MyApp.getInstance().getApplicationContext(),user);
            sendMsgToView(user);
        }
        if (callback==null)
        {
            sendMsgToView(BaseResponse.class,result);
        }else {
            callback.onSuccess(loginResponse);
        }
    }

    @Override
    public <V> void getObject(V v) {
        super.getObject(v);
        user= (User) v;
        RequestParams parems=new RequestParams(Constant.uploadImg);
        parems.addBodyParameter("clientId",Constant.CLIENT_ID);
        parems.addBodyParameter("accessToken", user.getAccessToken());
        parems.addBodyParameter("clientSecret",Constant.CLIENT_SECRET);
        if ( user.getHeadUrl_local()!=null)
        {
            parems.setMultipart(true);
            parems.addBodyParameter("imgFile", new File( user.getHeadUrl_local()));
            sendToServer(parems,1);
        }else {
            BaseResponse baseResponse=new BaseResponse();
            baseResponse.setMessage("图片路径错误");
            sendMsgToView(baseResponse);
        }
    }
    }
